package com.otheima.localdatetimeformatter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {//格式化和解析
    public static void main(String[] args) {
        //格式化 将LocalDateTime转为想要的格式
        //初始化时间
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        System.out.println(localDateTime);
        //设置格式
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //格式化
        String format = localDateTime.format(pattern);
        System.out.println(format);

        //解析 将字符串转为LocalDateTime格式
        String s ="2021-08-05 08:05:13";
        LocalDateTime newLocalDateTime = LocalDateTime.parse(s, pattern);
        System.out.println(newLocalDateTime.getYear());
    }
}
